﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using G15_Agent.Models.Travel;
using System.Net;

namespace G15_Agent.BUS
{
    public class BUS_Travel
    {
        public Service.Travel_G11.TravelService serviceG11 { get; set; }

        public BUS_Travel()
        {
            serviceG11 = new Service.Travel_G11.TravelService();
        }

        #region WSTravel0 - Đăng nhập

        public bool DangNhap_G11(string userName, string password)
        {
            try
            {
                CookieContainer cookieJar = new CookieContainer();
                serviceG11.CookieContainer = cookieJar;
                return serviceG11.WSTravel0(userName, password);
            }
            catch (System.Exception ex)
            {
                string logError = ex.ToString();
            }
            return false;
        }

        #endregion

        #region WSTravel1 - Lâý danh sách tour du lịch theo tiêu chí

        public List<TourDuLich> LayDanhSachTourDuLichTheoTieuChi_G11(string loaiTour, string diemXuatPhat)
        {
            List<TourDuLich> result = new List<TourDuLich>();
            try
            {
                Service.Travel_G11.SearchTourResult[] dsTourDuLich = serviceG11.WSTravel1(loaiTour, diemXuatPhat);
                foreach (Service.Travel_G11.SearchTourResult serverItem in dsTourDuLich)
                {
                    TourDuLich clientItem = new TourDuLich();

                    //serverItem.AddedDate;
                    clientItem.gioiHanSoLuongNguoi = serverItem.BookingLimit;
                    clientItem.soLuongDaDat = serverItem.CurrentBooking;
                    clientItem.giaTour = serverItem.CusPrice;
                    clientItem.soNgay = serverItem.Days;
                    clientItem.moTa = serverItem.Description.Trim();
                    clientItem.hanChotDatVe = serverItem.EndBookingDate;
                    clientItem.ngayDi = serverItem.FromDate;
                    clientItem.noiXuatPhat = serverItem.FromPlaceID.ToString().Trim();
                    clientItem.maTour = serverItem.ID.ToString().Trim();
                    clientItem.duongDanAnh = serverItem.Image.Trim();
                    clientItem.soDem = serverItem.Nights;
                    clientItem.tinhTrang = serverItem.Status.Trim();
                    clientItem.tenTour = serverItem.Title.Trim();
                    clientItem.ngayVe = serverItem.ToDate;
                    clientItem.noiDen = serverItem.ToPlaceID.ToString().Trim();
                    clientItem.loaiTour = serverItem.TourCatID.ToString().Trim();

                    result.Add(clientItem);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            

            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        #region WSTravel2 - Tìm kiếm tour du lịch mở rộng

        public List<TourDuLich> TimKiemTourDuLichMoRong_G11(string loaiTour, DateTime ngayDi,
            DateTime ngayVe, double giaToiThieu, double giaToiDa, string diaDiem)
        {
            List<TourDuLich> result = new List<TourDuLich>();
            try
            {
                Service.Travel_G11.SearchTourResult[] dsTourDuLich = serviceG11.WSTravel2(loaiTour, ngayDi, ngayVe, (float)giaToiThieu, (float)giaToiDa, diaDiem);
                foreach (Service.Travel_G11.SearchTourResult serverItem in dsTourDuLich)
                {
                    TourDuLich clientItem = new TourDuLich();

                    //serverItem.AddedDate;
                    clientItem.gioiHanSoLuongNguoi = serverItem.BookingLimit;
                    clientItem.soLuongDaDat = serverItem.CurrentBooking;
                    clientItem.giaTour = serverItem.CusPrice;
                    clientItem.soNgay = serverItem.Days;
                    clientItem.moTa = serverItem.Description.Trim();
                    clientItem.hanChotDatVe = serverItem.EndBookingDate;
                    clientItem.ngayDi = serverItem.FromDate;
                    clientItem.noiXuatPhat = serverItem.FromPlaceID.ToString().Trim();
                    clientItem.maTour = serverItem.ID.ToString().Trim();
                    clientItem.duongDanAnh = serverItem.Image.Trim();
                    clientItem.soDem = serverItem.Nights;
                    clientItem.tinhTrang = serverItem.Status.Trim();
                    clientItem.tenTour = serverItem.Title.Trim();
                    clientItem.ngayVe = serverItem.ToDate;
                    clientItem.noiDen = serverItem.ToPlaceID.ToString().Trim();
                    clientItem.loaiTour = serverItem.TourCatID.ToString().Trim();

                    result.Add(clientItem);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            
            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        #region WSTravel3 - Lâý thông tin chi tiết tour

        public TourDuLichChiTiet LayThongTinChiTietTour_G11(string maTour)
        {
            TourDuLichChiTiet result = new TourDuLichChiTiet();

            try
            {
                Service.Travel_G11.TourDuLichChiTiet serverItem = serviceG11.WSTravel3(int.Parse(maTour))[0];
                result.dsHoatDong = serverItem.activity;
                result.dsMayBay = serverItem.airplane;
                result.moTa = serverItem.description;
                result.ngayDen = serverItem.end_date;
                result.noiDen = serverItem.end_point;
                result.ngayDi = serverItem.from_date;
                result.dsKhachSan = serverItem.hotel;
                result.soNgay = serverItem.num_day;
                result.soDem = serverItem.num_night;
                result.dsDiaDiem = serverItem.place;
                result.noiXuatPhat = serverItem.start_point;
                result.loaiTour = serverItem.tour_cat;
                result.maTour = serverItem.tour_id;
                result.giaTour = serverItem.tour_price;
                result.tenTour = serverItem.tour_title;
                result.dsXeDi = serverItem.transport;
            }
            catch (System.Exception ex)
            {
                return null;
            }


            return result;
        }

        #endregion

        #region WSTravel4 - Gửi yêu cầu mua tour

        public int GuiYeuCauMuaTour_G11(int maTour, int soLuongNguoiLon, int soLuongTreEm, List<KhachHang> dsNguoiDi)
        {
            Service.Travel_G11.AgentTourCusDetail[] dsNguoiDi4Server = new Service.Travel_G11.AgentTourCusDetail[dsNguoiDi.Count];
            for (int i = 0; i < dsNguoiDi.Count; i++)
            {
                dsNguoiDi4Server[i].Age = dsNguoiDi[i].tuoi;
                dsNguoiDi4Server[i].AgentTourDetail = null;
                dsNguoiDi4Server[i].AgentTourID = maTour;
                dsNguoiDi4Server[i].CusFirstName = dsNguoiDi[i].hoTen;
                dsNguoiDi4Server[i].CusLastName = "";
                dsNguoiDi4Server[i].ExpDate = null;
                dsNguoiDi4Server[i].ID = int.Parse(dsNguoiDi[i].maKhachHang);
                dsNguoiDi4Server[i].Identification = dsNguoiDi[i].cmnd;
                dsNguoiDi4Server[i].IssuingCountry = dsNguoiDi[i].diaChi;
                dsNguoiDi4Server[i].PassportNumber = dsNguoiDi[i].passport;
            }
            try
            {
                return serviceG11.WSTravel4(maTour, soLuongNguoiLon, soLuongTreEm, dsNguoiDi4Server);
            }
            catch (System.Exception ex)
            {
                return 0;
            }
            
        }

        #endregion

    }
}